//
//  WNavigationViewController.m
//  WLWVOW
//
//  Created by 环球巨人 on 2017/3/11.
//  Copyright © 2017年 com.SXUG. All rights reserved.
//

#import "WNavigationViewController.h"

@interface WNavigationViewController ()

@end

@implementation WNavigationViewController

// 只初始化一次
+ (void)initialize {
    // 设置项目中item的主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];

    // Normal
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

    // 不可用状态
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = WRGBColor(123, 123, 123);
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateDisabled];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (iOS7Later) {
        self.navigationBar.barTintColor = WRGBColor(39, 58, 248);
    }else {
        self.navigationBar.tintColor = WRGBColor(39, 58, 248);
    }

    UIColor* color = [UIColor whiteColor];

    NSDictionary* dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationBar.titleTextAttributes= dict;

}

/**
 *  重写这个方法目的：能够拦截所有push进来的控制器
 *
 *  @param viewController 即将push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count > 0) {
        // 自动隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;

        UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [leftBtn setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);//上左下右
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];

        // 定义leftBarButtonItem
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];

        // 定义rightBarButtonItem
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
    }

    // 调用父类pushViewController，self.viewControllers数组添加对象viewController
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)more
{
    [self popToRootViewControllerAnimated:YES];
}




//// 判断是否为iOS7
//#define iOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
//
//+ (void)initialize
//{
//    // 1.取出设置主题的对象
//    UINavigationBar *navigationBar = [UINavigationBar appearance];
//
//    // 2.设置导航栏的返回按钮颜色及背景图片
//    if (iOS7) {
//        navigationBar.tintColor = [UIColor whiteColor];
//    }
//
//    UIImage *navBarBg = [[UIImage imageNamed:@"navBg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
//    [navigationBar setBackgroundImage:navBarBg forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//
//    //[navigationBar setBackgroundColor:[UIColor redColor]];
//    //[navigationBar setBarTintColor:[UIColor yellowColor]];
//
//    [navigationBar setShadowImage:[[UIImage alloc] init]];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
//
//    // 3.标题
//#ifdef __IPHONE_7_0
//    [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]/*, NSFontAttributeName:[UIFont systemFontOfSize:18]*/}];
//#else
//    [navigationBar setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]/*, NSFontAttributeName:[UIFont systemFontOfSize:18]*/}];
//#endif
//}

@end
